<template>
  <div>
    <template v-if="getType === 'select'">
      <el-select
        style="width:327px"
        v-model="typeValue"
        placeholder="请选择"
        @change="handleUpdateValue"
        @focus="handleSelectFocus"
        clearable
      >
        <el-option
          v-for="item in optionList"
          :key="item.DETAIL_CODE"
          :label="item.DETAIL_NAME"
          :value="item.DETAIL_CODE"
        ></el-option>
      </el-select>
    </template>
    <template v-else-if="getType === 'date'">
      <el-date-picker
        style="width: 327px"
        v-model="typeValue"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        type="date"
        placeholder="选择日期"
        @change="handleUpdateValue"
      >
      </el-date-picker>
    </template>
    <template v-else-if="getType === 'int'">
      <el-input
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 8}"
        clearable
        placeholder="请输入内容"
        v-model="typeValue"
        @change="handleUpdateValue"
      >
      </el-input>
    </template>
    <template v-else-if="getType === 'text'">
      <el-input
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 8}"
        clearable
        placeholder="请输入内容"
        v-model="typeValue"
        @change="handleUpdateValue"
      >
      </el-input>
    </template>
    <template v-else>
      <div>暂无此类型</div>
    </template>
  </div>
</template>

<script>
import { queryStandardDataDetailsByCode } from "@/api/bianqian/tagApi";
import store from "@/store";
export default {
  name: "CustumType",
  props: {
    value: {
      type: [String, Number],
      default: null,
    },

    row: {
      type: Object,
      default: () => {},
    },
  },

  watch: {
    value(val) {
      this.typeValue = val;
    },
    row: {
      deep: true,

      handler(e) {
        const { tagCondition } = e;
        this.selectCodeList(e)
        this.typeValue = tagCondition;
      },
    },
  },

  computed: {
    getType() {
      const { tfieldType } = this.row || {};
      let typeObj = {
        select: "select",
        date: "date",
        text: "text",
        int: "text",
      };
      return typeObj[tfieldType] || "";
    },
  },

  data() {
    return {
      typeValue: "",
      optionList: [],
    };
  },

  mounted() {
    const { tagCondition = "" } = this.row;
    this.typeValue = String(tagCondition);
    // console.log(this.row);
    this.selectCodeList(this.row);
  },
  methods: {
    handleSelectFocus() {
      this.selectCodeList();
    },
    selectCodeList(e = {}) {
      const { tdictCode = "" } = e;
      // console.log('this.row', e)
      if(!tdictCode) return
      const params = {
        userId: store.getters.user_id.toString(),
        userName: store.getters.userName.toString(),
        token: store.getters.token.toString(),
        dictCode: tdictCode,
      };
      queryStandardDataDetailsByCode(params).then((result) => {
        let list = [];
        if (Array.isArray(result)) {
          list =result.map(e => {
          return  {...e, DETAIL_CODE: String(e.DETAIL_CODE) }
        })
        }
        this.optionList = list;
      });
    },
    // 总的逻辑来处理数据
    handleUpdateValue(e) {
      // console.log(e, "e");
      this.$emit("value", e);
      this.$emit("change", e);
    },
  },
};
</script>

<style  scoped>
</style>